<template>
    <div class="news">
        <div class="tagList">
            <div class="tag">
                <div class="item" :class="index == tagIndex ? 'tagActive' : ''" v-for="tag, index in tagList"
                    :key="index" @click="queryByNavigationId(tag.navigationId, index)">
                    <span>{{ tag.navigationName }}</span>
                    <div :class="index == tagIndex ? 'triangle' : ''"></div>
                </div>
            </div>
            <div class="more" @click="more">
                更多<i class="el-icon-arrow-right"></i>
            </div>
        </div>
        <div class="tableList">
            <div class="info" v-for="item, index in tableList" :key="index" @click="jumpNavigation(item)">
                <div class="left">
                    <div class="point"></div>
                    <span class="label ellipsis">{{ item.title }}</span>
                </div>
                <div class="time">
                    {{ item.publishTime }}
                    <div v-show="$isToday(item.publishTime)" class="newInfo">new</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import homepageApi from '@/api/homepage';
export default {
    name: 'App',
    components: {},
    props: {
        tagList: {
            type: Array,
            default: () => {
                return [];
            },
        },
    },
    data() {
        return {
            tagIndex: 0,
            infoList: [
                {
                    info: '夏德才同志现场观摩分局网球俱乐部集训',
                    time: '03-15'
                }, {
                    info: '丁一同志慰问治安支队工',
                    time: '03-15'
                }, {
                    info: '唐怡冰同志专题研究爱警暖警工作',
                    time: '03-15'
                }, {
                    info: '张亚宏同志率队至玉佛寺检查指导春节安保维稳工作并',
                    time: '03-01'
                }, {
                    info: '夏德才同志参与筹备市局组队参加2024全国警察网球',
                    time: '02-20'
                }, {
                    info: '张骏同志接待上海公安学院学习考察团',
                    time: '02-04'
                },
            ],
        }
    },
    mounted() {
        if (this.tagList.length > 0) this.queryByNavigationId(this.tagList[0].navigationId);
    },
    computed: {
        //避免样式崩塌，只获取前6个公告图片
        tableList() {
            return this.infoList.slice(0, 6)
        }
    },
    watch: {
        tagList: {
            handler(v) {
                console.log(v)
            },
            deep: true
        }
    },
    methods: {
        //选择tag
        chooseTag(index) {
            this.tagIndex = index;
            //异步请求...
        },
        async queryByNavigationId(navigationId, index = 0) {
            if (!navigationId) return;
            this.navigationId = navigationId
            this.tagIndex = index;
            let { data } = await homepageApi.queryByNavigationId(navigationId);
            this.infoList = data || [];
        },
        jumpNavigation(item) {
            console.log(item)
            if (item.jumpType == 'URL' && item.url) {
                window.open(item.url, '_blank');
            } else if (item.jumpType == 'ARTICLE' && item.articleId) {
                this.$router.push({
                    path: '/navigation', query: {
                        articleId: item.articleId,
                        name: '文章'
                    }
                })
            }

        },
        more() {
            let data = this.tagList[this.tagIndex]
            this.$router.push({
                path: '/navigationList', query: {
                    name: data.navigationName,
                    navigationId: data.navigationId
                }
            })
        },
    }
}
</script>

<style lang="scss" scoped>
/* 三角 */
.triangle {
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-top: 8px solid #3e73c7;
}

.ellipsis {
    width: 200px;
    /* 设置你需要的宽度 */
    white-space: nowrap;
    /* 防止文本换行 */
    overflow: hidden;
    /* 隐藏超出容器的内容 */
    text-overflow: ellipsis;
    /* 显示省略号 */
}

.news {
    width: 100%;
    height: 260px;
    margin-bottom: 20px;

    .tagList {
        width: 100%;
        height: 40px;
        background-color: #ecf3ff;
        padding-right: 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;

        .tag {
            display: flex;
            font-weight: bold;
            font-size: 16px;

            .item {
                padding: 0 20px;
                line-height: 40px;
                cursor: pointer;
                position: relative;
            }
        }

        .triangle {
            position: absolute;
            bottom: -6px;
            left: 50%;
            transform: translate(-50%);
        }

        .more {
            font-size: 14px;
            cursor: pointer;
        }

        .tagActive {
            color: #ffffff;
            background: linear-gradient(to right bottom, #5386d7, #3e73c7);
        }
    }

    .tableList {
        padding-top: 15px;

        .time {
            width: 160px;
            text-align: right;
            position: relative;

            .newInfo {
                font-size: 10px;
                line-height: 14px;
                padding: 0 5px;
                border-radius: 7px;
                color: #ffffff;
                background-color: #ff0000;
                right: 0;
                top: -6px;
                position: absolute;
            }
        }

        .info {
            width: 100%;
            border-bottom: 1px solid #cecece00;
            font-size: 14px;
            text-align: left;
            line-height: 34px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;

            &:hover {
                transform: translate(-3px);
                border-bottom: 1px solid #e7e7e7;
            }

            .left {
                width: calc(100% - 165px);
                display: flex;
                justify-content: flex-start;
                align-items: center;

                .point {
                    width: 4px;
                    height: 4px;
                    background-color: #cecece;
                    margin-right: 5px;
                }
            }
        }

        .label {
            width: 100%;
            color: #333333;
            font-weight: bold;
        }
    }

}
</style>